package oo

import (
	"elegantGo/chapter02-1/pkg/validate"
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

type Code struct {
	value string
}

func (o *Code) SetValue(v string) {
	o.value = v
}

func (o *Code) Value() string {
	return o.value
}

func (o *Code) IsValid() bool {
	return validate.Var(o.value, fmt.Sprintf("number,len=%d", o.length())) == nil
}

func (o *Code) Generate() {
	v := ""
	for i := 0; i < o.length(); i++ {
		v += strconv.Itoa(rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10))
	}
	o.value = v
}

func (o *Code) length() int {
	return 4
}
